package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.mapper.UserMapper;
import com.starlink.model.request.system.UserVipInfoRequest;
import com.starlink.model.vo.system.UserVipInfoVO;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.UserVipSetDtoMapper;
import com.starlink.model.domain.UserVipSetDO;
import com.starlink.mapper.UserVipSetMapper;
import com.starlink.model.request.UserVipSetRequest;
import com.starlink.service.UserVipSetService;
import com.starlink.model.util.UserVipSetConvert;
import com.starlink.model.vo.UserVipSetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.common.enums.VipEnum.VIP_CUSTOM_DEFINITION;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class UserVipSetServiceImpl extends ServiceImpl<UserVipSetMapper, UserVipSetDO> implements UserVipSetService {
    @Autowired
    private UserVipSetMapper userVipSetMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVipSetVO getByKey(long id) {
        UserVipSetVO userVipSetVO = UserVipSetDtoMapper.INSTANCE.do2VO(getById(id));
        return userVipSetVO;
    }

    @Override
    public PageInfo<UserVipSetVO> pageQuery(Pageable pageable,UserVipSetRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<UserVipSetDO> list = userVipSetMapper.pageQuery(request);
        List<UserVipSetVO> userVipSetVOList = UserVipSetConvert.buildUserVipSetVoList(list);
        return new PageInfo(userVipSetVOList);
    }

    @Override
    public List<UserVipSetVO> listQuery(UserVipSetRequest request) {
        List<UserVipSetDO> list = userVipSetMapper.selectDOList(UserVipSetDtoMapper.INSTANCE.request2DO(request));
        List<UserVipSetVO> userVipSetVOS = UserVipSetConvert.buildUserVipSetVoList(list);
        return userVipSetVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(UserVipSetRequest request) {
        BigDecimal amount = request.getAmount();
        Integer numberOfYears = request.getNumberOfYears();
        Integer timeLimit = request.getTimeLimit();
        AssertUtil.assertNotNull(amount,"金额不能为空!");
        AssertUtil.assertNotNull(timeLimit,"有效期类型不能为空!");
        if (VIP_CUSTOM_DEFINITION.getType().equals(timeLimit)) {
            AssertUtil.assertNotNull(numberOfYears, "年限不能为空!");
        }
        save(UserVipSetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(UserVipSetRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(UserVipSetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        userVipSetMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public UserVipSetVO getVipInfo() {
        UserVipSetDO userVipSetDO =  this.lambdaQuery().one();
        return UserVipSetDtoMapper.INSTANCE.do2VO(userVipSetDO);
    }

    @Override
    public PageInfo<UserVipInfoVO> pageQueryVipUser(Pageable pageable, UserVipInfoRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<UserVipInfoVO> list = userMapper.pageQueryVipUser(request);
        return new PageInfo(list);
    }
}
